<?php

namespace app\api\controller\paotui\poster;

use addons\paotui\service\Poster;
use app\admin\model\paotui\Posterextract;
use app\admin\model\paotui\PosterExtractType;
use think\Db;
use think\Validate;

/**
 * 佣金接口
 */
class Money extends PosterApi
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = "*";

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取可提现金额、今日收益、今日完成订单、本周收益
     */
    public function info(){

        $posterService = new Poster();
        $data['allowExtractMoney'] = $posterService->getAllowExtractMoney($this->auth->id);                    //可提现金额
        $data['weekMoney'] = $posterService->getWeekMoney($this->auth->id);                                    //获取本周收益
        $data['todayMoney'] = $posterService->getTodayMoney($this->auth->id);                                  //今日收益

        $this->success("请求成功",$data);
    }

    /**
     * 获取最少提现金额
     */
    public function getMinExtractMoney(){
        $posterModel = new \app\admin\model\paotui\Poster();
        $posterInfo = $posterModel->where(['id'=>$this->auth->id,'status'=>1])->find();
        if(empty($posterInfo)){
            $this->error("当前账号已被锁定，请联系客服");
        }

        $minExtractMoney = \addons\paotui\service\Config::get('min_extract_money');
        $this->success("请求成功",$minExtractMoney);
    }

    /**
     * 获取服务费率
     */
    public function fee_scale(){
        $this->success("请求成功",round(\addons\paotui\service\Config::get('fee_scale')/100,2));
    }

    /**
     * 佣金记录
     */
    public function index(){
        $page = $this->request->post("page");
        $limit = $this->request->post("limit");

        $where = ['money_log.poster_id'=>$this->auth->id];
        $posterService = new Poster();
        $data = $posterService->getMoneyLogList($where,$page,$limit);
        $this->success("请求成功",$data);
    }


    /**
     * 提现记录
     */
    public function extract_list(){
        $page = $this->request->post("page");
        $limit = $this->request->post("limit");

        $where = ['poster_id'=>$this->auth->id];
        $posterService = new Poster();
        $data = $posterService->getExtractList($where,$page,$limit);
        $this->success("请求成功",$data);
    }

    /**
     * 申请提现
     */
    public function extract(){
        $money = $this->request->post("money");
        $type = $this->request->post("type");
        $posterModel = new \app\admin\model\paotui\Poster();
        $posterInfo = $posterModel->where(['id'=>$this->auth->id,'status'=>1])->find();
        if(empty($posterInfo)){
            $this->error("当前账号已被锁定，请联系客户");
        }

        //查看最小提交金额
        $minExtractMoney = \addons\paotui\service\Config::get('min_extract_money');
        if($money < $minExtractMoney){
            $this->error("最小提现金额为".$minExtractMoney."元");
        }

        Db::startTrans();
        $posterService = new Poster();
        $allowExtractMoney = $posterService->getAllowExtractMoney($this->auth->id);

        if($money > $allowExtractMoney){
            $this->error("当前最大可提现金额为".$allowExtractMoney."元");
        }

        $extractTypeModel = new PosterExtractType();
        $extractTypeInfo = $extractTypeModel->where(['poster_id'=>$this->auth->id])->find();
        if(empty($extractTypeInfo)){
            $this->error("请先维护提现信息！");
        }
        switch ($type){
            case 1: //微信
                $data['wechat'] = $extractTypeInfo['wechat'];
                $rule = [
                    'wechat'  => 'require',
                ];
                break;
            case 2: //支付宝
                $data['alipay'] = $extractTypeInfo['alipay'];
                $data['alipay_name'] = $extractTypeInfo['alipay_name'];
                $rule = [
                    'alipay'  => 'require',
                    'alipay_name'  => 'require',
                ];
                break;
            case 3: //银行卡
                $data['bank_name'] = $extractTypeInfo['bank_name'];
                $data['bank_code'] = $extractTypeInfo['bank_code'];
                $data['name'] = $extractTypeInfo['name'];
                $rule = [
                    'bank_name'  => 'require',
                    'bank_code'  => 'require',
                    'name'=>'require',
                ];
                break;
            default:
                $this->error("请选择提现方式！");
                break;

        }

        $msg = [
            'bank_name.require' => '请完善开户行！',
            'bank_code.require' => '请完善银行卡号！',
            'name.require' => '请完善账户名称！',
            'wechat.require' => '请完善微信号！',
            'alipay.require' => '请完善支付宝账号！',
            'alipay_name.require' => '请完善支付宝姓名！'
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error(__($validate->getError()), null,[]);
        }

        $data['type'] = $type;
        $data['poster_id'] = $this->auth->id;
        $data['extract_price'] = $money;
        $data['fee_scale'] = bcmul(round(\addons\paotui\service\Config::get('fee_scale')/100,4),100);
        $data['true_price'] = bcsub($money,$money*round(\addons\paotui\service\Config::get('fee_scale')/100,4),2);
        $data['mark'] = "佣金提现";

        $posterExtractModel = new Posterextract();
        if(!$posterExtractModel->save($data)){
            $this->error("提现申请失败，请稍候再试或联系客服");
        }

        Db::commit();
        $this->success("提现申请成功，请等待审核通过");
    }

}
